<?php
	/**
	 * Error catching utilities.
	 * @author IMP
	 */
	
	abstract class ErrorCatcher {
		private static $oldHandler = null,
					   $caught = array(),
					   $begun = false;
		
		public static final function beginCatching($types = E_ALL){
			self::$oldHandler = set_error_handler(__CLASS__ . '::__error', $types);
			self::$begun = true;
		}
		
		public static final function caughtErrors(){
			return count(self::$caught) > 0;
		}
		
		public static final function grabErrors(){
			if(self::$begun){
				$ret = self::$caught;
				self::$caught = array();
				set_error_handler(self::$oldHandler);
				self::$begun = false;
				
				return $ret;
			}
		}
		
		public static final function __error($no, $str, $file, $line, $context){
			self::$caught[] = array('code' => $no,
									 'message' => $str,
									 'file' => $file,
									 'line' => $line,
									 'context' => $context);
		}
	}
?>